import { createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk'

const reducer = (state, { type, list, item, index }) => {
    const NewState = JSON.parse(JSON.stringify(state))
    switch (type) {
        case "CHANGE_LIST":
            NewState.list = list
            return NewState
        case "ADD_LIST_ITEM":
            NewState.list.push(item)
            return NewState
        case "DEL_LIST_ITEM":
            NewState.list.splice(index, 1)
            return NewState
        default:
            return state
    }
}

const initState = {
    list: []
}

const store = createStore(reducer, initState, applyMiddleware(thunk))

export default store